<?php
class Adminuser extends Base_Controller
{
    public function __construct()
    {
        /******待编辑******/
        $this->_page_size = 20;
        $this->_form_type = 'admin_user';
        $this->_list_where = array();
        $this->_action_list = array('EDIT');
        $this->_controller = 'admin/adminuser/';
        parent::__construct();
    }

    public function showList($page = 1)
    {
        $limit = $this->_page_size;
        $offset = ($page-1) * $limit;
        $pdata = $this->getPagination($this->_controller.'showList/');

        $res = $this->getList($offset,$limit);
        $createbtn = $this->load->view('admin/form/createbtn',array('controller'=>site_url($this->_controller)),true);

        $data = array('headers'=>$res['headers'],'list'=>$res['list'],
            'pagination'=>$pdata['pagination'],'createbtn'=>$createbtn);
        $content = $this->load->view('admin/form/list',$data,true);

        echo $this->getAdminView($content);

    }

    public function update($id = 0)
    {
        $post = $this->input->post();
        if(empty($post)){
            $form = $this->updateBuildForm($id);
            $content = $this->load->view('admin/form/edit',array('controller'=>site_url($this->_controller),'form'=>$form),true);
            echo $this->getAdminView($content);
        }else{
            /******待编辑******/
            //filter post data
            $post['email'] = trim($post['email']);
            $post['userName']= trim($post['userName']);
            $post['privilege']= (int)trim($post['privilege']);
            $post['userToken']= md5(trim($post['userToken']));
//            var_dump($_FILES);exit();
            $res = $this->uploadImg('userIcon',140,140);
            if ($res['code'] == 1){
                $post['userIcon'] = $res['imgurl'][0];
            }
            $post['changed'] = time();

            $this->_update_data = $post;
            parent::update($id);
        }
    }

    public function create()
    {
        $post = $this->input->post();
        if(empty($post)){
            $form = $this->createBuildForm();
            $content = $this->load->view('admin/form/edit',array('controller'=>site_url($this->_controller),'form'=>$form),true);
            echo $this->getAdminView($content);
        }else{
            /******待编辑******/
            //filter post data
            $post['email'] = trim($post['email']);
            $post['userName']= trim($post['userName']);
            $post['privilege']= (int)trim($post['privilege']);
            $post['userToken']= md5(trim($post['userToken']));
            $res = $this->uploadImg('userIcon',140,140);
            if ($res['code'] == 1){
                $post['userIcon'] = $res['imgurl'][0];
            }else{$post['userIcon']='';}

            $post['created'] = time();
            $post['changed'] = time();
            //为管理员创建普通用户
//            $user['utype'] = 'admin';
//            $user['userName'] = $post['userName'];
//            $user['userIcon'] = $post['userIcon'];
//            $user['email'] = $post['email'];
//            $user['emailActive'] = 1;
//            $user['userToken'] = $post['userToken'];
//            $user['created'] = time();
//            $user['changed'] = time();
//            $user['status'] = 1;
//            $this->load->model('user_model');
//            $commid = $this->user_model->createUser($user);
//            if ($commid !== false){
//                $post['commid'] = $commid;
//            }else{die('create common user failed');}

            $this->_create_data = $post;
            parent::create();
        }
    }

    public function delete($id = 0){return false;}

}